package com.sofwin.system.controller;

import com.sofwin.Constants;
import com.sofwin.pojo.SysUser;
import com.sofwin.system.service.MenuService;
import com.sofwin.system.service.UserService;
import com.sofwin.vo.MenuVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @packageName: com.sofwin.system.controller
 * @user: andyliu
 * @date: 2021/12/14 11:34
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;


    @GetMapping
    public String index(HttpSession session, Model model){
        Subject subject = SecurityUtils.getSubject();//
        String loginName = subject.getPrincipal().toString();//获取用户名
        SysUser user = new SysUser();
        user.setLoginAccount(loginName);
        user = userService.checkLogin(user);
        Integer roleId = user.getRoleId();
        // 根据用户对应的角色Id查询改用户所拥有的资源信息

        List<MenuVO> menus = menuService.queryMenusByRoleId(roleId);
        // 资源信息放入request作用域
        model.addAttribute("menus",menus);
        return "index";
    }
}
